﻿using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
using Lucene.Net.Index;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TDSContentCore.Engine;

namespace TDSContentApp.Converters
{
    public class Converter_PPTX : IFileToStringConverter
    {
        public string Extension { get; } = ".PPTX";

        public Converter_PPTX()
        {
        }


        public string Convert(string path)
        {
            return ExtractText(path).ToString();
        }

        public void Dispose()
        {

        }

        public static StringBuilder ExtractText(string filename)
        {
            var sb = new StringBuilder();
     
            // Open the presentation as read-only.
            using (PresentationDocument presentationDocument = PresentationDocument.Open(filename, false))
            {
                for (int i = 0; i < presentationDocument.PresentationPart.SlideParts.Count(); i++)
                {
                    GetAllTextInSlide(sb, presentationDocument, i);
                }
            }        

            return sb;
        }
    public static void GetAllTextInSlide(StringBuilder sb,PresentationDocument presentationDocument, int slideIndex)
        {
            // Verify that the presentation document exists.
            if (presentationDocument == null)
            {
                throw new ArgumentNullException("presentationDocument");
            }

            // Verify that the slide index is not out of range.
            if (slideIndex < 0)
            {
                throw new ArgumentOutOfRangeException("slideIndex");
            }

            // Get the presentation part of the presentation document.
            PresentationPart presentationPart = presentationDocument.PresentationPart;

            // Verify that the presentation part and presentation exist.
            if (presentationPart != null && presentationPart.Presentation != null)
            {
                // Get the Presentation object from the presentation part.
                Presentation presentation = presentationPart.Presentation;

                // Verify that the slide ID list exists.
                if (presentation.SlideIdList != null)
                {
                    // Get the collection of slide IDs from the slide ID list.
                    var slideIds = presentation.SlideIdList.ChildElements;

                    // If the slide ID is in range...
                    if (slideIndex < slideIds.Count)
                    {
                        // Get the relationship ID of the slide.
                        string slidePartRelationshipId = (slideIds[slideIndex] as SlideId).RelationshipId;

                        // Get the specified slide part from the relationship ID.
                        SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId);

                        // Pass the slide part to the next method, and
                        // then return the array of strings that method
                        // returns to the previous method.
                        GetAllTextInSlide(sb, slidePart);
                    }
                }
            }
            // Else, return null.
            return;
        }

        public static void GetAllTextInSlide(StringBuilder sb, SlidePart slidePart)
        {
            // Verify that the slide part exists.
            if (slidePart == null)
            {
                throw new ArgumentNullException("slidePart");
            }


            // If the slide exists...
            if (slidePart.Slide != null)
            {
                // Iterate through all the paragraphs in the slide.
                foreach (var paragraph in slidePart.Slide.Descendants<DocumentFormat.OpenXml.Drawing.Paragraph>())
                {


                    // Iterate through the lines of the paragraph.
                    foreach (var text in paragraph.Descendants<DocumentFormat.OpenXml.Drawing.Text>())
                    {
                        // Append each line to the previous lines.
                        if (!string.IsNullOrWhiteSpace(text.Text))
                        {
                            sb.AppendLine(text.Text);
                        }
                    }                 
                }
            }          
        }


    }
}
